package com.music.service.impl;

import com.laoxuai.beans.annotation.Autowired;
import com.laoxuai.beans.annotation.Service;
import com.laoxuai.ibatis.util.Example;
import com.laoxuai.ibatis.util.PageInfo;

import com.laoxuai.util.StringUtils;
import com.music.dao.MusicDao;
import com.music.pojo.Artist;
import com.music.pojo.Lyric;
import com.music.pojo.Music;
import com.music.service.MusicService;

import java.sql.SQLException;
import java.util.List;

@Service
public class MusicServiceImpl implements MusicService {

    @Autowired
    private MusicDao musicDao;

    @Override
    public int insert(Music music) {
        if (music.getName()==null) {
            return -1;//歌曲名不能为null
        }

        Integer newID;
        Integer maxID = null;
        try {
            maxID = musicDao.selectMaxId();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        if (maxID == 0) {
                newID = maxID+1;
            } else {
                newID = maxID + 1;
            }
            music.setId(newID);

            music.setIsDeleted(1);



        try {
            musicDao.inset_music_artist(newID,music.getArtist());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }


            try {
                return musicDao.insert(music);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        return 0;
    }

    @Override
    public int delete(Integer id) {
        try {
            Music remusic=musicDao.getByPrimaryKey(id);
            if(remusic==null){
                return -1;//当id传入的不存在时返回-1
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        try {
            return musicDao.delete(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int update(Music music) {
        Integer id=music.getId();
        try {
            Music remusic=musicDao.getByPrimaryKey(id);//当歌手不存在时返回-1
            if(remusic==null){
                return -1;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        try {
            return musicDao.update(music);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public Music getByPrimaryKey(Integer id) {
        try {
            return musicDao.getByPrimaryKey(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Music> listPage(Music music, int page, int size) {
        try {
            return musicDao.listPage(music, page, size);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public PageInfo<Music> listPageInfo(Music music, int page, int size) {
        try {
            return musicDao.listPageInfo(music, page, size);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Music> selectByExample(Music music) {
        Example example = createExmaple(music);
        try {
            return musicDao.selectByExample(example);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    private Example createExmaple(Music music) {
        Example example = new Example(Music.class);
        Example.Criteria criteria = example.createCriteria();
        // TODO
        if(music != null){
            if(!StringUtils.isEmpty(music.getId())){
                criteria.andEqualTo("id", music.getId());
            }
            if(!StringUtils.isEmpty(music.getArtist())){
                criteria.andEqualTo("artist", music.getArtist());
            }
            if(!StringUtils.isEmpty(music.getName())){
                criteria.andLike("name", "%"+music.getName()+"%");
            }
            if(!StringUtils.isEmpty(music.getAlbum())){
                criteria.andEqualTo("album", music.getAlbum());
            }
            if(!StringUtils.isEmpty(music.getDuration())){
                criteria.andEqualTo("duration", music.getDuration());
            }
            if(!StringUtils.isEmpty(music.getImage())){
                criteria.andEqualTo("image", music.getImage());
            }
            if(!StringUtils.isEmpty(music.getCreateTime())){
                criteria.andEqualTo("createTime", music.getCreateTime());
            }
            if(!StringUtils.isEmpty(music.getUpdateTime())){
                criteria.andEqualTo("updateTime", music.getUpdateTime());
            }
            if(!StringUtils.isEmpty(music.getIsDeleted())){
                criteria.andEqualTo("isDeleted", music.getIsDeleted());
            }
        }
        return example;
    }

    /**
     * Music 构建查询对象
     * @param music
     * @return
     */
    public Example createExample(Music music){
        Example example = new Example(Music.class);
        Example.Criteria criteria = example.createCriteria();
        if(music != null){
            if(!StringUtils.isEmpty(music.getId())){
                criteria.andEqualTo("id", music.getId());
            }
            if(!StringUtils.isEmpty(music.getArtist())){
                criteria.andEqualTo("artist", music.getArtist());
            }
            if(!StringUtils.isEmpty(music.getName())){
                criteria.andLike("name", "%"+music.getName()+"%");
            }
            if(!StringUtils.isEmpty(music.getAlbum())){
                criteria.andEqualTo("album", music.getAlbum());
            }
            if(!StringUtils.isEmpty(music.getDuration())){
                criteria.andEqualTo("duration", music.getDuration());
            }
            if(!StringUtils.isEmpty(music.getImage())){
                criteria.andEqualTo("image", music.getImage());
            }
            if(!StringUtils.isEmpty(music.getCreateTime())){
                criteria.andEqualTo("createTime", music.getCreateTime());
            }
            if(!StringUtils.isEmpty(music.getUpdateTime())){
                criteria.andEqualTo("updateTime", music.getUpdateTime());
            }
            if(!StringUtils.isEmpty(music.getIsDeleted())){
                criteria.andEqualTo("isDeleted", music.getIsDeleted());
            }
        }
        return example;
    }


    @Override
    public List<Music> selectAll() {
        try {
            return musicDao.selectAll();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }
}
